package com.contory.ssn_lgnd.framework.gl;

import android.opengl.GLES20;
import android.util.Log;

public class ESShaderLoader {
	int shaderId;
	int type;
	String shaderSrc;
	
	public void load(int type, String shaderSrc){
		this.type = type; 
		this.shaderSrc = shaderSrc;
		shaderId = loadShader(type, shaderSrc);
	}
	
	private int loadShader(int type, String shaderSrc) {
		int shader;
		int[] compiled = new int[1];

		// Create the shader object
		shader = GLES20.glCreateShader(type);

		if (shader == 0)
			return 0;

		// Load the shader source
		GLES20.glShaderSource(shader, shaderSrc);

		// Compile the shader
		GLES20.glCompileShader(shader);

		// Check the compile status
		GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);

		if (compiled[0] == 0) {
			Log.e("ESShader", GLES20.glGetShaderInfoLog(shader));
			GLES20.glDeleteShader(shader);
			return 0;
		}
		return shader;
	}
	
	//public void setProperties(int type, String shaderSrc){
	//	this.type = type; 
	//	this.shaderSrc = shaderSrc;
	//}
	
	public int getShaderId(){
		return shaderId;
	}
	
	public void unload(){
		GLES20.glDeleteShader(shaderId);
	}
}
